import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {Provider} from 'mobx-react';
import {BrowserRouter, StaticRouter} from 'react-router-dom';
import {Switch} from 'react-router';
import Route from './route/Router';
import lyby from 'lyby';
import lybyShijue from 'lyby/shijue';


class EmptyLayout extends Component{
    render(){
        return (
            <div>
                {this.props.children}
            </div>
        )
    }
}
export default class Root extends Component {
    static propTypes = {
        layout: PropTypes.func,
        states: PropTypes.object,
        routes: PropTypes.object || PropTypes.array,
        type: PropTypes.string
    };

    componentDidMount() {
        // window.__INITIAL_STATE__ = null;
    }

    render() {
        const {url, states, routes, type, layout} = this.props;
        let LayoutApp = layout || EmptyLayout;
        let needLogin = false;
        let routeRenders = (routesParam) => {
            let enterWithoutLoginRouteKeys = Object.keys(routesParam);
            return enterWithoutLoginRouteKeys.map(function (item, index) {
                let obj = routesParam[item];

                let rOptins = {
                    exact : true,
                    key : item,
                    path : obj.path,
                    component : obj.component
                }
                if (obj.needLogin) {
                    needLogin = obj.needLogin;
                }
                if(obj.routeCmp){
                    let CurRoute = obj.routeCmp;
                    return (
                        <CurRoute {...rOptins}/>
                    )
                }else{
                    return (
                        <Route {...rOptins}/>
                    );
                }
            })
        };
        let routesArray = routeRenders(routes);
        if(needLogin){
            if(!states){
                console.log('没有数据');
                return null;
            }
            let validateFlag = false;
            if(states.loginUserStore.id){
                validateFlag = true;
            }
            if(needLogin instanceof Function){
                validateFlag = needLogin(states);
            }
            if(!validateFlag){
                if(typeof(window) != 'undefined'){
                    lybyShijue.logout().then(()=>{
                        let cLocation = lyby.getCurrentLocation();
                        cLocation.href = '/user/login?redirect=' + encodeURIComponent(cLocation.href);
                    }).catch(err=>{
                        alert(err)
                    });
                }
                return null;
            }
        }
        // if(needLogin){
        //     routesArray.push.apply(routesArray,routeRenders(loginRoutes))
        // }
        return (
            <Provider {...states}>
                {
                    type === 'server' ? (
                        <StaticRouter location={url} context={{}}>
                            <LayoutApp>
                                <Switch>
                                    {routesArray}
                                </Switch>
                            </LayoutApp>
                        </StaticRouter>
                    ) : (
                        <BrowserRouter>
                            <LayoutApp>
                                <Switch>
                                    {routesArray}
                                </Switch>
                            </LayoutApp>
                        </BrowserRouter>
                    )
                }
            </Provider>
        );
    }
}
